package Classes.Tanks 
{
	import flash.utils.Timer;
	import net.flashpunk.Entity;
	import flash.geom.Point;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.FP;
	import Classes.Collisions.*;
	import Classes.Bullet.*;
	
	/**
	 * ...
	 * @author Alex
	 */
	public class Tank extends Entity
	{
		private var m_Move_Behavior:IMoveBehavior;
		private var m_speed:Number;
		private var m_accel:Number;
		private var m_hitpoints:int;
		private var m_directionFacing:String;
		private var m_directionMoving:String;
		private var m_pic:Image;
		private var m_countBulletsFired:int;
		private var m_timeSinceLastShot:Timer;
		private var m_rateOfFire:int;

		public var m_status:String;		
		public var m_currentLocation:Point;
		
		public function Tank(startLocation:Point) 
		{
			m_speed = 2;
			m_accel = 1;
			m_hitpoints = 0;
			m_countBulletsFired = 0;
			m_rateOfFire = 4;
			m_timeSinceLastShot = new Timer(0);
			m_timeSinceLastShot.start();
			m_directionFacing = Direction.RIGHT;
			m_currentLocation = new Point();
			
			m_status = TankStatus.DEAD;

			m_currentLocation = startLocation;
			setLocation(m_currentLocation);
		}
		public function setLocation(newLocation:Point):void
		{
			m_currentLocation = newLocation;
			this.x = m_currentLocation.x;
			this.y = m_currentLocation.y;
		}
		
		public function setImage(image:Image):void
		{
			m_pic = image;
			graphic = m_pic;
		}
				
		public function setMoveBehaviour(moveBehavior:IMoveBehavior):void
		{
			m_Move_Behavior = moveBehavior;
		}
		
		public function setDirectionFacing(newDirection:String):void
		{
			var image:Image = m_pic;
			image.centerOrigin();
			
			var newAngle:int = 0;
			
			if (newDirection == Direction.UP)
			{
				newAngle = 90;
				m_directionFacing = Direction.UP;
			}
			if (newDirection == Direction.DOWN)
			{
				newAngle = 270;
				m_directionFacing = Direction.DOWN;
			}
			if (newDirection == Direction.LEFT)
			{
				newAngle = 180;
				m_directionFacing = Direction.LEFT;
			}
						
			if (newDirection == Direction.RIGHT)
			{
				newAngle = 0;	
				m_directionFacing = Direction.RIGHT;
			}
			image.angle = newAngle;
			setHitbox(image.width, image.height,image.x,image.y);
			setImage(image);
			
		}
		
		public function getDirectionMoving():String
		{
			return m_directionFacing;
		}
		
		
		public function moveTankForward():void
		{
			var newPosition:Point = new Point();
			newPosition = m_currentLocation;
			var speed:int = m_speed * m_accel + FP.elapsed;
			var status:String = m_status;
			if (status == TankStatus.MOVING)
			{
				if (m_directionFacing == Direction.UP)
					newPosition.y -= speed;
				if (m_directionFacing == Direction.DOWN)
					newPosition.y += speed;			
				if (m_directionFacing == Direction.LEFT)
					newPosition.x -= speed;			
				if (m_directionFacing == Direction.RIGHT)
					newPosition.x += speed;
			}	
			setLocation(newPosition);	
		}
		
		public function getCollisions():Array
		{	
			var collisionDetector:CollisionDetector = new CollisionDetector();
			var typesCollidedWith:Array = collisionDetector.getCollisions(this);
			
			return typesCollidedWith;
			
		}
		
		public function LevelCollisionHandling():void
		{
			m_status = TankStatus.STOPPED;
			var directionMoving:String = getDirectionMoving();
			var levelCollision:LevelCollision = new LevelCollision();
			var adjustedLocation:Point = levelCollision.getCoordsAfterCollision(directionMoving,height,width, m_currentLocation);
			
			setLocation(adjustedLocation);
		}
		
		public function Shoot():void
		{
			var framesPerSecond:Number = 30;
			
						
			if ((m_countBulletsFired == 0) || (m_timeSinceLastShot.currentCount>(framesPerSecond*m_rateOfFire)))
			{
				var pointFired:Point = new Point();
				pointFired.x = this.centerX;
				pointFired.y = this.centerY;
				var directionFired:String = this.m_directionFacing;
				FP.world.add(new Bullet(pointFired, directionFired));
				m_countBulletsFired += 1;
				m_timeSinceLastShot.reset();
				m_timeSinceLastShot.start();
			}
	
		}
		
		
		

	}
	
}